﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using KBManagement.DAL;
using KBManagement.BIZ;
using KBManagement.Library;


namespace KBManagement
{

    
    public partial class ManageCustomer : Form,InforObserver
    {

        KBSQLClassDataContext context;
        CustomerFacade customerFacade;
        int dgvCustomerIndex = 0;
        public ManageCustomer()
        {
            Utils.CreateInstance().AddInforObserver(this);
            context = Utils.CreateInstance().GetDBContext();
            customerFacade = new CustomerFacade();
            InitializeComponent();
            SelectCustomer();
        }

        private void SelectCustomer()
        {
            var customer = from fc in context.Customers
                           select new
                           {
                               fc.ID,
                               fc.FullName,
                               fc.Email,
                               fc.Adddress,
                               fc.Phone,
                           };
            dataGridView1.DataSource = customer;
            dataGridView1.Columns[0].HeaderText = "Mã số";
            dataGridView1.Columns[0].AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells;
            dataGridView1.Columns[1].HeaderText = "Họ tên";
            dataGridView1.Columns[2].HeaderText = "Thư điện tử";
            dataGridView1.Columns[3].HeaderText = "Địa chỉ";
            dataGridView1.Columns[4].HeaderText = "Điện thoại";
            toolStripBtnEditCus.Enabled = false;
            sửaToolStripMenuItem.Enabled = false;
            try
            {
                dataGridView1.CurrentCell = dataGridView1[0, dgvCustomerIndex];
            }
            catch (Exception)
            {

            }
            //employeeQuery = from fc in context.Employees select fc;
        }

        public void Update()
        {
            SelectCustomer();
        }

        private void toolStripBtnAddCus_Click(object sender, EventArgs e)
        {
            AddCustomer();
        }

        public void AddCustomer()
        {
            DiaLogEditAddCustomer dialog = new DiaLogEditAddCustomer(null);
            dialog.ShowDialog();
        }

        public void EditCustomer()
        {
            if (dataGridView1.SelectedRows.Count > 0)
            {
                try
                {
                    Customer cus = customerFacade.Find(dataGridView1.SelectedRows[0].Cells[0].Value);
                    DiaLogEditAddCustomer dialog = new DiaLogEditAddCustomer(cus);
                    dialog.ShowDialog();
                }
                catch (Exception)
                {
                }
            }
        }

        private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            toolStripBtnEditCus.Enabled = true;
            sửaToolStripMenuItem.Enabled = true;
            dgvCustomerIndex = e.RowIndex;
        }

        private void toolStripBtnEditCus_Click(object sender, EventArgs e)
        {
            EditCustomer();   
        }

        private void thêmToolStripMenuItem_Click(object sender, EventArgs e)
        {
            AddCustomer();
        }

        private void sửaToolStripMenuItem_Click(object sender, EventArgs e)
        {
            EditCustomer();
        }
    }
}
